package com.tanhua.server.service;

import com.tanhua.domain.mongo.Video;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class VideoMQService {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference
    private VideoApi videoApi;

    public void sendSaveVideoMsg(String videoId){
        sendMessage(videoId, 1);
    }

    public void sendLikeVideoMsg(String videoId){
        sendMessage(videoId, 2);
    }

    public void sendUnlikeVideoMsg(String videoId) {
        sendMessage(videoId, 3);
    }

    public void sendCommentVideoMsg(String videoId) {
        sendMessage(videoId, 4);
    }

    public void sendMessage(String videoId, Integer type){
        Video video = videoApi.findById(videoId);

        Map<String, Object> message = new HashMap<>();
        message.put("userId", UserHolder.getUser().getId());
        message.put("videoId", video.getVid());
        message.put("type", type);

        rocketMQTemplate.convertAndSend("tanhua-video", message);
        log.info("发送日志消息，topic:{}，message:{}", "tanhua-video", message);
    }
}